<?php
// Include prepend.inc to load Qcodo
require('prepend.inc.php');
require('header.inc.php');
require('Panel/pnlHeader.php');
//require('SharedetailsListPanel.class.php');
require_once('libs/Security/login.class.php');
require_once('libs/Debug/DebugWriter.php');
//require_once('Zend/Http/Cookie.php');

// Security check for ALLOW_REMOTE_ADMIN
// To allow access REGARDLESS of ALLOW_REMOTE_ADMIN, simply remove the line below
QApplication::CheckRemoteAdmin(); ?>
<?php
class Dashboard extends QForm
{
	protected $lnkMyLibrary;
	protected $lnkAmazonSearch;
	protected $lnkAddContact;
	protected $lnkShareAssets;
	protected $lnkSignOut;
	protected $lnkRegister;
	protected $pnlTitle;
	protected $pnlList;
	protected $pnlEdit;
	protected $pnlHeader;
	protected $txtSearchTermLend;
	protected $btnSearchLend;
	protected $dtgShareDetails;
	protected $pxyEdit;
	
	protected $txtUserId;
	protected $txtPassword;
	protected $btnLogin;
	protected $btnSignUp;
	protected $pnlMenu;
	protected $chkRemember;

	protected function Form_Run()
	{
		QApplication::$LoadedPage = 'Home';
		QApplication::CheckRemoteAdmin();
		isset($_SESSION['User'])?$_SESSION['User'] =$_SESSION['User']:$_SESSION['User'] = '';

		if($_SESSION['User'] <> '')
			QApplication::Redirect('postlogin.php');
		
	}
	
	protected function Form_Create()
	{
		QApplication::$LoadedPage = 'Home';
		$this->pnlTitle = new QPanel($this);
		$this->pnlTitle->Text = __VEDA_VERSION__;

		$this->pnlList = new QPanel($this, 'pnlList');
		$this->pnlList->AutoRenderChildren = true;

		$this->pnlEdit = new QPanel($this, 'pnlEdit');
		$this->pnlEdit->AutoRenderChildren = true;
		$this->pnlEdit->Visible = true;

		$this->objDefaultWaitIcon = new QWaitIcon($this);
			
		$this->lnkMyLibrary = new QLinkButton($this);
		$this->lnkMyLibrary->Text = "My Library";
		$this->lnkMyLibrary->AddAction(new QClickEvent(),new QServerAction('lnkMyLibrary_Click',$this->objDefaultWaitIcon));
			
		$this->lnkAmazonSearch = new QLinkButton($this);
		$this->lnkAmazonSearch->Text = "Add an Asset";
		$this->lnkAmazonSearch->AddAction(new QClickEvent(),new QServerAction('lnkAmazonSearch_Click',$this->objDefaultWaitIcon));
			

		$this->lnkAddContact = new QLinkButton($this);
		$this->lnkAddContact->Text = "Add a new Contact";
		$this->lnkAddContact->AddAction(new QClickEvent(),new QServerAction('lnkAddContact_Click',$this->objDefaultWaitIcon));
		
		$this->lnkShareAssets = new QLinkButton($this);
		$this->lnkShareAssets->Text = "Share your Assets";
		$this->lnkShareAssets->AddAction(new QClickEvent(),new QServerAction('lnkShareAssets_Click',$this->objDefaultWaitIcon));


		/**
		 * Create the Login and signup elements here 
		 * 
		**/
		$this->txtUserId_Create();
		$this->txtPassword_Create();
		$this->btnLogin_Create();
		$this->lnkRegister_Create();
		$this->lnkSignOut_Create();
		
		$this->pnlHeader_Create();
		//Create the Checkbox to Remember
		$this->chkRemember_Create();
		//$this->dtgShareDetails_Create();
	}

	
	protected function txtUserId_Create()
	{
		$this->txtUserId = new QTextBox($this->pnlTitle);
		$this->txtUserId->Name = 'User Id';
	}
	
	protected function txtPassword_Create()
	{
		$this->txtPassword = new QTextBox($this->pnlTitle);
		$this->txtPassword->Name = 'Password';
		$this->txtPassword->TextMode = QTextMode::Password;
		$this->txtPassword->AddAction(new QEnterKeyEvent(),new QServerAction('Login'));
	}
	
	protected function btnLogin_Create()
	{
		$this->btnLogin = new QButton($this->pnlTitle);
		$this->btnLogin->Text = 'Login';
		$this->btnLogin->AddAction(new QClickEvent(),new QServerAction('Login'));

	}
	
	
	protected function chkRemember_Create()
	{
		$this->chkRemember = new QCheckBox($this);
		$this->chkRemember->Text = QApplication::Translate('Remember Me');
	}
	
	protected function lnkRegister_Create()
	{
		$this->lnkRegister = new QLinkButton($this);
		$this->lnkRegister->Text = 'Sign Up';
		$this->lnkRegister->AddAction(new QClickEvent(),new QServerAction('Register_Click'));
	}

	protected function lnkSignOut_Create()
	{
		$this->lnkSignOut = new QLinkButton($this);
		$this->lnkSignOut->Text = 'SignOut';
		$this->lnkSignOut->AddAction(new QClickEvent(),new QServerAction('SignOut_Click'));
	}
	
	
	protected function SignOut_Click()
	{
		$_SESSION['User'] = '';
		QApplication::Redirect('index.php');
	}
	
	protected function pnlMenu_Create()
	{
		$this->pnlMenu = new QPanel($this,'pnlMenu');
		$this->pnlMenu->AutoRenderChildren = true;
		$this->pnlMenu->Visible = true;
	}
	
	
	protected function lnkAmazonSearch_Click()
	{
		QApplication::Redirect('AmazonSearch.php');
	}
	
	protected function Login()
	{
		/*$cookie = new Zend_Http_Cookie('Esteemed',$this->txtUserId->Text,'180','localhost','/',true);
		if(isset($cookie))
		{
			QApplication::DisplayAlert('Cookie is not set');
		}*/
		
		$objChkLogin = new Login();
		$blnVal = $objChkLogin->Login_Validate($this->txtUserId->Text,$this->txtPassword->Text);
		
		if($blnVal == 'Right')
		{
			$_SESSION['User'] = $_SESSION['objUser']->MemberId;
			QApplication::Redirect('postlogin.php');
		}
		else
		{
			$_SESSION['User'] = '';
			QApplication::DisplayAlert('Incorrect User/Password');
		}
		
		
		
	}
	
	
	protected function Form_Validate()
	{
		// By default, we report that Custom Validations passed
		$blnToReturn = true;

		// Custom Validation Rules
		// TODO: Be sure to set $blnToReturn to false if any custom validation fails!

		$blnFocused = false;
		foreach ($this->GetErrorControls() as $objControl) {
			// Set Focus to the top-most invalid control
			if (!$blnFocused) {
				$objControl->Focus();
				$blnFocused = true;
			}

			// Blink on ALL invalid controls
			$objControl->Blink();
		}

		return $blnToReturn;
	}

	protected function Register_Click()
	{
		QApplication::Redirect('register.php');
	}
	
	
	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}
	
	
	public function CloseEditPane($blnUpdatesMade) {
			// Close the Edit Pane
			$this->pnlEdit->RemoveChildControls(true);
			$this->pnlEdit->Visible = false;

			// If updates were made, let's "brute force" the updates to the screen by just refreshing the list pane altogether
			if ($blnUpdatesMade)
				$this->pnlList->Refresh();
		}

		
	public function SetEditPane(QPanel $objPanel = null) 
		{
			$this->pnlEdit->RemoveChildControls(true);
			if ($objPanel) {
				$objPanel->SetParentControl($this->pnlEdit);
				$this->pnlEdit->Visible = true;
			} else {
				$this->pnlEdit->Visible = false;
			}
		}
}

Dashboard::Run('Dashboard');

?>

<?php require (__INCLUDES__ . '/footer.inc.php'); ?>